package cn.itcast.bos.web.action;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.utils.MailUtils;
import cn.itcast.bos.utils.SmsUtils;
import cn.itcast.bos.web.base.BaseAction;
import cn.itcast.crm.domain.Customer;
@Controller
@Namespace("/")
@ParentPackage("json-default")
//@Scope("prototype")
public class CustomerAction extends BaseAction<Customer>{

	@Action("customer_sendSms")
	public String sendSms() throws UnsupportedEncodingException{
		//获取随机生成的验证码
		String randomCode=RandomStringUtils.randomNumeric(4);
		// 电话号码和 验证码存放到session中
		ServletActionContext.getRequest().getSession()
		.setAttribute(model.getTelephone(), randomCode);
		
		// 编辑短信内容  调用方法发送短信
		String msg="尊敬的用户你好本次获取到的验证码是:"+randomCode+"服务电话400-500-888";
		//String result=SmsUtils.sendSmsByHTTP(model.getTelephone(), msg);	
		System.out.println(msg);
		//根据返回值进行处理
		/*if(result.startsWith("000")){
			return NONE;
		}else{
			throw new RuntimeException();
		}*/
		return NONE;
	}
	
	
	
	
	//用户的注册功能
	@Autowired
	private RedisTemplate<String, String>redisTemplate;

	private String checkCode;
			
	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}


	@Action(value="customer_signup",
			results={@Result(name="success",type="redirect",location="signup-success.html"),
			@Result(name="input",type="redirect",location="signup.html")			
		})
	public String customerSignup(){
		// 短信验证码的校验
		String code=(String) ServletActionContext.getRequest().getSession().getAttribute(model.getTelephone());
		System.out.println(code);
		if(code==null||!checkCode.equals(code)){
			System.out.println("验证码错误");
			return NONE;
		}
		
		// 发送邮件 生成激活码
				String activeCode=RandomStringUtils.randomNumeric(32);
				// 激活码保存到redis中 
				redisTemplate.opsForValue().set(model.getEmail(), activeCode,24,TimeUnit.HOURS);
				// 调用方法发送邮件
				String  content = "尊敬的用户你好,点击连接绑定邮箱<a href='"+MailUtils.activeUrl+"?email="+model.getEmail()
						+"&activeCode="+activeCode+"'>快来点我啊</a>";
				MailUtils.sendMail("快递速运激活邮件", content, model.getEmail(),"98765");
		System.out.println("邮件发送成功");
		
		//webservice 将customer 的数据保存
		System.out.println(model);
		WebClient.create("http://localhost:8090/crm_management/"
				+ "services/customerService/customer")
				.type(MediaType.APPLICATION_JSON).post(model);
		System.out.println("客户注册成功");
		
		return SUCCESS;
	}
}

























